#!/bin/bash
# This script is released under the GNU General Public License 3.0
# Check the COPYING file included with this distribution

# partition_mainmenu
# first menu for partitioning. Auto-partition or manually?
# parameters: none
#
# returns $ERROR_CANCEL=64 on user cancel
# anything else is a real error
# reason: show_dialog() needs a way to exit "Cancel"
#
# writes menus and noise to STDERR

# location of other scripts to source
readonly SHAREDIR="$(dirname ${0})" || exit $?

# source common variables, functions and error handling
source "${SHAREDIR}"/common.sh || exit $?

#####################
## begin execution ##

NEWSELECTION=
SELECTION='0'
RETSUB=
MENU_ITEMS=
MAXSELECTION="0"

# CONFIG_LIST: One string with 0 or more items of defined FSspec:
CONFIG_LIST=
CONFIG_LIST_NEW=

while true; do
	# define menu items
	MENU_ITEMS=("0" "Auto-Prepare (erases the ENTIRE hard drive)" \
		"1" "Manually partition hard drives" \
		"2" "Fine-tune partition setup (add, edit, delete, encryption)"
		)
	# check if root partition is defined
	if "${SHAREDIR}"/FSspec list_haskeyvalue "${CONFIG_LIST}" 'mountpoint' '/'; then
		MENU_ITEMS+=("3" "Finish partition setup")
	else
		[ "${MAXSELECTION}" -ge 3 ] && MAXSELECTION=2
	fi

	# expand menu items array below
	NEWSELECTION="$(show_dialog --default-item "${SELECTION}" --menu "Prepare Hard Drive" \
		0 0 0 "${MENU_ITEMS[@]}")" || exit $?
	# call subscript by selected item
	case "${NEWSELECTION}" in
		# autoprepare
		"0")
			CONFIG_LIST_NEW="$("${SHAREDIR}"/partition_autoprepare)"
			# capture sub script exit
			RETSUB=$?
			if [ "${RETSUB}" -eq 0 ]; then
				# merge old config with new one
				CONFIG_LIST="$("${SHAREDIR}"/FSspec merge "${CONFIG_LIST}" "${CONFIG_LIST_NEW}")" || exit $?
			fi
			;;
		# manual setup
		"1")
			CONFIG_LIST_NEW="$("${SHAREDIR}"/partition_manual)"
			# capture sub script exit
			RETSUB=$?
			if [ "${RETSUB}" -eq 0 ]; then
				# nothing to merge, but remove inexistent devices
				CONFIG_LIST="$("${SHAREDIR}"/FSspec merge "${CONFIG_LIST}" "${CONFIG_LIST_NEW}")" || exit $?
			fi
			;;
		# change configurations (filesystems, mountpoint, encryption, etc.)
		"2")
			CONFIG_LIST_NEW="$("${SHAREDIR}"/partition_configurations "${CONFIG_LIST}")"
			# capture sub script exit
			RETSUB=$?
			if [ "${RETSUB}" -eq 0 ]; then
				# use result as new config list
				CONFIG_LIST="${CONFIG_LIST_NEW}"
			fi
			;;
		# Create filesystems, then exit this menu
		"3")
			# prints new configs to STDOUT on success
			"${SHAREDIR}"/partition_finalise "${CONFIG_LIST}"
			# capture sub script exit
			RETSUB=$?
			if [ "${RETSUB}" -eq 0 ]; then
				exit "${RETSUB}"
			fi
			;;

		*)
			echo "ERROR: Unexpected response '${NEWSELECTION}' in $(basename $0)" 1>&2
			exit 1
			;;
	esac
	# handle errors from sub-script/functions using a common utility function
	if ! catch_menuerror "$(basename $0)" "${NEWSELECTION}" "${RETSUB}"; then
		# everything ok, increase selection for next menu item
		# jump from auto-prepare directly to mountpoints
		if [ "${SELECTION}" -eq 0 ]; then
			SELECTION=2
		else
			SELECTION="$((NEWSELECTION+1))" || exit $?
		fi
		if [ "${MAXSELECTION}" -lt "${SELECTION}" ]; then
			MAXSELECTION="${SELECTION}"
		fi
	fi
done
